# -*- coding: utf-8 -*-
#  @author  Bink
#  @date  2020/9/2 20:03
# @Email : 2641032316@qq.com

import pandas as pd
import time, datetime

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('max_colwidth', 100)

file_dorm = pd.read_table("D:/pra/Python/python-programming-project/src/resources/train/1.txt",
# file_dorm = pd.read_table("D:/pra/Python/python-programming-project/src/resources/train/dorm_train.txt",
                          dtype=str,
                          delimiter=','
                          )
df_dorm = pd.DataFrame(file_dorm)
df_dorm.columns = ['id', '具体时间', '进出方向(0进寝室，1出寝室)']
# df_dorm_by_id = df_dorm.groupby('id')
# print(df_dorm_by_id.apply())
# print(df_dorm)


df_dorm['具体时间'] = (df_dorm['具体时间'].apply(lambda x:
                                         int(time.mktime(time.strptime(x, "%Y/%m/%d %H:%M:%S")))
                                         )
                   *
                   df_dorm['进出方向(0进寝室，1出寝室)'].apply(lambda x:
                                                    -1 if int(x) == 0
                                                    else
                                                    1
                                                    )
                   )

df_dorm = df_dorm.iloc[:, :-1]
# print(df_dorm.groupby('id'))
df_dorm2 = df_dorm.groupby('id').count().reset_index()['具体时间']
# df_dorm = df_dorm.groupby('id').sum().reset_index()
# df_dorm['具体时间'] = df_dorm['具体时间'].apply(lambda timeStamp:
#                                         time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(timeStamp))
#                                         )
df_dorm['进出次数'] = df_dorm2
df_dorm = df_dorm.groupby('id').count().reset_index()['id']
# df_dorm.to_excel("./out/dormInfo.xlsx", index=None)
df_dorm.to_excel("./out/dormInfoDemo1part.xlsx", index=None)
print(df_dorm)
# print(type(df_dorm))
